/**
 * 
 */
package bounjour;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Vector;

/**
 * @author Senem Beken
 * @date: 22.05.11
 * 
 * This class contains functions that are responsible for
 * returning tags associated with a post after sorting
 * them by their weights
 */
public class TagManager 
{
	private static RecordManager rm = RecordManager.getRecordManager();
	private int PostID = 0;
	
	public TagManager(int postID)
	{
		this.PostID = postID;
	}
	
	public ArrayList <Tag> GetAllTagNames()
	{
		ArrayList <Tag> listTagNames = new ArrayList <Tag>();
		String sqlQuery = "SELECT COUNT(*) as TagWeight, Tags.Name from PostTags INNER JOIN Tags ON " +
						  "PostTags.TagId = Tags.Id WHERE PostID = " + 
						  PostID + " GROUP BY Tags.Name ORDER BY tagWeight";
				
		try
		{
			Vector <HashMap<String, Object>>records = rm.selectRecord(sqlQuery);
			
			for (Enumeration <HashMap<String, Object>>e = records.elements(); e.hasMoreElements();)
			{
				HashMap <String, Object> resultMap = e.nextElement();
			
			    String tagName = (String) resultMap.get("Name");
			    Long tagWeight = (Long) resultMap.get("TagWeight");
			    
			    Tag t = new Tag(tagName, tagWeight.intValue());
				listTagNames.add(t);
			}
		}
		
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		
		return listTagNames;
	}
}